import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.contacts.ContactEdge;
import pulpcore.animation.BindFunction;
import pulpcore.math.CoreMath;
import pulpcore.sprite.Sprite;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.pulp.body.ImageBodySprite;
import pulpfizz.pulp.body.Java2DBodySprite;
import pulpfizz.pulp.body.PhysicsLayer;

import java.awt.*;

/**
 * A basic high-speed bullet. Its body is always angled in the orientation of its velocity.
 * Created by will
 * Date: Apr 19, 2008 7:15:24 AM
 */
public class Shot extends ImageBodySprite {
    Sprite shadow;

    public static final float SHOT_SHARPNESS = 1;
    private static final float restitution = 2;
    private static final float friction = 0;
    private static final float density = 5;
    private static final float linearDamping = 0.5f;
    private static final float angularDamping = 0.4f;
    static Material m = new Material(friction, restitution, density, linearDamping, angularDamping);

    /** Fires a shot at the specified target. All inputs are physics layer coordinates. */
    public Shot(PhysicsLayer physics, float x, float y, float r, Vec2 target, double speed) {
        super(BodyUtils.createCircle(physics.getWorld(), x, y, r),physics, "steelball.png");

        // Image size is 66x83, actual circle is 60x60, offset 2,2
//        setSize(width.get() * (66.0 / 60.0),height.get() * (83.0 / 60.0));
        m.applyAll(body);
        body.setBullet(true);
        this.pushTowards(target.x,target.y,speed);
        getOld();
        setNaturalSize();
//        image.setHotspot(32,32);

        Color greyshadow = new Color(50, 50, 50, 100);
        shadow = new Java2DBodySprite(body,physics,1, greyshadow,greyshadow,greyshadow);
//        shadow = new FilledSprite(this.x.get(),this.y.get(),this.width.get(),this.height.get(), Colors.rgba(Colors.BLACK,100));
        shadow.x.bindTo(posX());
        shadow.y.bindTo(posY());
        shadow.bindSizeTo(this);
        shadow.angle.bindTo(this.angle);
        shadow.alpha.bindTo(this.alpha);

    }

    public double naturalWidth;
    public double naturalHeight;

    private void setNaturalSize() {
        naturalWidth = width.get();
        naturalHeight = height.get();
    }

    private void getOld() {
        oldX = this.x.get();
        oldY = this.y.get();
    }

    /** Fires a shot in the specified direction and angle. All inputs are physics layer coordinates. */
    public Shot(PhysicsLayer physics,float x, float y, float r, double angle, double speed) {
        super(BodyUtils.createCircle(physics.getWorld(), x, y, r),physics, "steelball.png");
//        setSize(width.get() * (66.0 / 60.0),height.get() * (83.0 / 60.0));

        m.applyAll(body);
        int dx = CoreMath.cos(CoreMath.toFixed(angle));
        int dy = CoreMath.sin(CoreMath.toFixed(angle));
        body.setBullet(true);
        this.push(CoreMath.toDouble(dx),CoreMath.toDouble(dy),speed);
        ContactEdge cont = body.m_contactList;
        getOld();
        setNaturalSize();

        Color greyshadow = new Color(50, 50, 50, 100);
        shadow = new Java2DBodySprite(body,physics,1, greyshadow,greyshadow,greyshadow);
//        shadow = new FilledSprite(this.x.get(),this.y.get(),this.width.get(),this.height.get(), Colors.rgba(Colors.BLACK,100));
        shadow.x.bindTo(posX());
        shadow.y.bindTo(posY());
        shadow.alpha.bindTo(this.alpha);
    }

    BindFunction posX() {
        return new BindFunction() {
            public Number f() {
                return (posX(body).f().doubleValue() +3);
            }
        };
    }
    BindFunction posY() {
        return new BindFunction() {
            public Number f() {
                return (posY(body).f().doubleValue() +3);
            }
        };
    }

    public Sprite getShadow() {
        return shadow;
    }
    

    /** (from ImageSprite) */
/*
    protected void drawSprite(CoreGraphics g) {
        g.setColor(Colors.GREEN);
        if (image != null) {
//            g.drawRotatedImage(image,0,0,width.getAsInt(),height.getAsInt(),CoreMath.toFixed(ang),0,0,image.getWidth(),image.getHeight());
//            g.drawRect(0,0,this.image.getWidth(),this.image.getHeight());
            g.drawImage(image);
        } else {
            g.drawRect(0,0,this.width.getAsInt(),this.height.getAsInt());
        }
    }
*/

    public double oldX, oldY, length;
    public double ang;
    public void update(int elapsedTime) {
        super.update(elapsedTime);
        double dX = this.x.get() - oldX;
        double dY = this.y.get() - oldY;
        length = new Vec2((float)dX,(float)dY).length();
        ang = CoreMath.atan2(CoreMath.toFixed(dY), CoreMath.toFixed(dX));
        getOld();
//        this.setSize(naturalWidth+Math.abs(dX),naturalHeight+Math.abs(dY));
    }
}
